<script setup lang="ts">
import {  delRoleAPI, getRoleDetailAPI, getRoleListAPI, getRoleUserAPI, getTreeListAPI } from '@/apis/systemRole';
import type { Role, RoleData, RoleUserData } from '@/types/systemRole';
import userIcon from '@/assets/user.svg'
import userIconActive from '@/assets/user-active.svg'
import { ref } from 'vue';
import { ElMessage, ElMessageBox, type TreeInstance } from 'element-plus';


const roleList = ref<Role[]>([])
const getRoleList = async () => {
  const res = await getRoleListAPI()
  roleList.value = res.data
  // 获取第一个角色详情
  const roleId = roleList.value[0].roleId as number
  getRoleDetail(roleId)
  getRoleUserAPI(roleId)

}
getRoleList()

// 点击激活交互
const activeIndex = ref(0)
const changeRole = (idx: number,roleId:number) => {
  activeIndex.value = idx
  // 获取角色详情
  getRoleDetail(roleId)
  getRoleUser(roleId)
}

// 获取tree权限列表
const treeList = ref<RoleData[]>([])
const getTreeList = async () => {
  const res = await getTreeListAPI()
  console.log(res);
  treeList.value = res.data
}
getTreeList()

// 获取角色详情
const userTotal = ref(0) //成员数量
const perms = ref<number[][]>([])
const getRoleDetail = async (roleId:number) => {
  const res = await getRoleDetailAPI(roleId)
  console.log(res);
  perms.value = res.data.perms
  userTotal.value = res.data.userTotal
}

const treeRefs = ref<TreeInstance[]>([])
// 切换时,重新设置树形组件选中
treeRefs.value.forEach((tree: TreeInstance, index: number) => {
  tree.setCheckedKeys(perms.value[index])
})

// tabs切换
const activeTabName = ref<'tree' | 'member'>('tree')

// 获取角色成员
const roleUserList = ref<RoleUserData[]>([])
const getRoleUser = async (roleId: number) => { 
  const res = await getRoleUserAPI(roleId)
  console.log(res);
  roleUserList.value = res.data.rows
}

// 删除角色
const delRole = async (id:number) => {
  ElMessageBox.confirm('您确定删除该角色吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    // 删除角色
    await delRoleAPI(id)
    // 获取角色列表
    getRoleList()
    // 轻提示
    ElMessage.success('删除成功')
  }).catch(() => { })
}
</script>

<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div class="role-item" v-for="(item, index) in roleList" :key="item.roleId"
      :class="{ active:index === activeIndex}" @click="changeRole(index,item.roleId as number)"
      >
        <div class="role-info">
          <img :src="index === activeIndex ? userIconActive : userIcon" alt="" class="icon">
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <img src="@/assets/more.svg" class="icon" style="border: none;"/>
            </span>
            <template #dropdown>
              <el-dropdown-item>
                <el-button link @click="$router.push(`/system/addRole/${item.roleId}`)">编辑角色</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button link @click="delRole(item.roleId as number)">删除</el-button>
              </el-dropdown-item>
            </template>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="small" @click="$router.push('/system/addRole')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeTabName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
        <!-- 角色权限列表 -->
         <div v-for="(item,index) in treeList" :key="item.id" class="tree-item">
          <div class="tree-title"> {{ item.title }} </div>
            <!-- 树形组件 -->
             <el-tree 
             :data="item.children"
             :props="{ label: 'title',disabled:()=>true }"
             show-checkbox
             default-expand-all
             :default-checked-keys="perms[index]"
             ref="treeRefs"
             node-key="id"
             >
             </el-tree>
         </div>
      </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员(${userTotal})`" name="menber">
          <div class="user-wrapper">
            <el-table :data="roleUserList" width="100%">
              <el-table-column label="序号" type="index"></el-table-column>
              <el-table-column label="员工姓名" prop="name"></el-table-column>
              <el-table-column label="登录账号" prop="userName"></el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<style scoped lang="scss">
.icon{
  width: 20px;
}
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;

  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
        width: 20px;
      }
    }

    .more {
      display: flex;
      align-items: center;
      .icon {
          width: 15px;
        }
    }

    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;

        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>